<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<div th:replace="common/common::html"></div>
<head>
    <meta charset="UTF-8">
    <link th:href="@{/assets/css/page.css}" rel="stylesheet"/>
    <title th:text="#{recycle.bin.title}"></title>
    <script th:inline="javascript">
        // 页面尺寸变量
        var _gridWidth;
        var _gridHeight;

        // 页面自适应
        function resizePageSize() {
            _gridWidth = $(document).width() - 12; /* 避免滚动条折行问题 */
            _gridHeight = $(document).height() - 65 - 40; /* 减去顶部菜单和查询区域高度 */
        }

        $(function () {
            resizePageSize();
            initRecycleGrid(); // 初始化表格
        });

        // 初始化回收站文件列表
        function initRecycleGrid() {
            var _columnWidth = (_gridWidth - 300) / 3;

            $("#recycle-grid").flexigrid({
                width: _gridWidth,
                height: _gridHeight,
                url: /*[[@{/recycle/queryRecycleFiles}]]*/'',
                dataType: 'json',
                colModel: [
                    {display: /*[[#{file.name}]]*/'', name: 'fileName', width: _columnWidth , sortable: true, align: 'left'},
                    {display: /*[[#{file.size}]]*/'', name: 'fileSize', width: _columnWidth, sortable: true, align: 'center'},
                    {display: /*[[#{recycle.delete.time}]]*/'', name: 'deleteTime', width: _columnWidth, sortable: true, align: 'center'},
                    {
                        display: /*[[#{common.operation}]]*/'',
                        name: 'fileId',
                        width: 150,
                        sortable: false,
                        align: 'center',
                        process: function(v, _trid, _row) {
                            var html = `<div class="operation-buttons">`;
                            if([[${#authorization.expression("hasRole('ROLE_cldk_file_recover')")}]]) {
                                html += `<a href="#" class="btn" onclick="recoverFile('${v}')" title=[[#{recycle.recover}]]><i class="glyphicon glyphicon-repeat"></i></a>`;
                            }
                            if([[${#authorization.expression("hasRole('ROLE_cldk_file_force_del')")}]]) {
                                html += `<a href="#" class="btn" onclick="permanentDelete('${v}', '${_row.fileName}')" title=[[#{recycle.permanent.delete}]]><i class="glyphicon glyphicon-trash"></i></a>`;
                            }
                            html += `</div>`;
                            return html;
                        }
                    }
                ],
                resizable: false,
                useRp: true,
                usepager: true,
                autoload: false, // 与userPage保持一致，手动触发加载
                hideOnSubmit: true,
                showcheckbox: true,
                rowbinddata: true
            });
            // 手动加载数据
            searchRecycleFiles();
        }

        // 搜索回收站文件
        function searchRecycleFiles() {
            var fileName = $("#search-file-name").val();
            var startTime = $("#start-time").val();
            var endTime = $("#end-time").val();

            $("#recycle-grid").flexOptions({
                params: [
                    {name: 'fileName', value: fileName},
                    {name: 'startTime', value: startTime},
                    {name: 'endTime', value: endTime}
                ]
            }).flexReload();
        }

        // 重置搜索条件
        function resetSearch() {
            $("#search-form")[0].reset();
            searchRecycleFiles();
        }

        // 恢复单个文件
        function recoverFile(fileId) {
            $.ajax({
                type: 'post',
                async: false,
                dataType: 'json',
                url: /*[[@{/recycle/recoverFile}]]*/'',
                data: [{name: "fileId", value: fileId}],
                success: function (data) {
                    message(data.msg);
                    if (data.success) {
                        $("#recycle-grid").flexReload();
                    }
                },
                error: function (msg) {
                    message(/*[[#{common.error}]]*/'');
                }
            });
        }

        // 批量恢复文件
        function batchRecoverFiles() {
            var ids = $("#recycle-grid").getFlexigridiAttrByIndex(0);
            if (ids.length === 0) {
                message(/*[[#{common.empty}]]*/'');
                return;
            }

            $.ajax({
                type: 'post',
                async: false,
                dataType: 'json',
                url: /*[[@{/recycle/batchRecoverFiles}]]*/'',
                data: [{name: "fileIds", value: ids.join(',')}],
                success: function (data) {
                    message(data.msg);
                    if (data.success) {
                        $("#recycle-grid").flexReload();
                    }
                },
                error: function (msg) {
                    message(/*[[#{common.error}]]*/'');
                }
            });
        }

        // 永久删除单个文件
        function permanentDelete(fileId, fileName) {
            $("#permanent-delete-file-id").val(fileId);
            $("#permanent-delete-file-name").text(fileName);
            $("#permanent-delete-dialog").dialog('open');
        }

        // 批量永久删除文件
        function batchPermanentDelete() {
            var ids = $("#recycle-grid").getFlexigridiAttrByIndex(0);
            if (ids.length === 0) {
                message(/*[[#{common.empty}]]*/'');
                return;
            }

            var fileNames = $("#recycle-grid").getFlexigridiAttrByIndex(1);
            $("#permanent-delete-file-id").val(ids.join(','));
            $("#permanent-delete-file-name").text(fileNames.join(','));
            $("#permanent-delete-dialog").dialog('open');
        }

        // 清空回收站
        function emptyRecycleBin() {
            $("#empty-recycle-confirm-dialog").dialog('open');
        }

        // 初始化对话框
        $(function() {
            // 永久删除确认对话框
            $("#permanent-delete-dialog").dialog({
                autoOpen: false,
                width: 400,
                modal: true,
                resizable: false,
                title: /*[[#{recycle.permanent.delete.confirm}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function () {
                            let fileIds = $("#permanent-delete-file-id").val();
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType: 'json',
                                url: /*[[@{/recycle/permanentDelete}]]*/'',
                                data: [{name: "fileIds", value: fileIds}],
                                success: function (data) {
                                    message(data.msg);
                                    if (data.success) {
                                        $("#permanent-delete-dialog").dialog('close');
                                        $("#recycle-grid").flexReload();
                                    }
                                },
                                error: function (msg) {
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });
                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function () {
                            $(this).dialog("close");
                        }
                    }
                ]
            });

            // 清空回收站确认对话框
            $("#empty-recycle-confirm-dialog").dialog({
                autoOpen: false,
                width: 400,
                modal: true,
                resizable: false,
                title: /*[[#{recycle.empty.confirm.title}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function () {
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType: 'json',
                                url: /*[[@{/recycle/emptyRecycleBin}]]*/'',
                                success: function (data) {
                                    message(data.msg);
                                    if (data.success) {
                                        $("#empty-recycle-confirm-dialog").dialog('close');
                                        $("#recycle-grid").flexReload();
                                    }
                                },
                                error: function (msg) {
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });
                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function () {
                            $(this).dialog("close");
                        }
                    }
                ]
            });
        });

        function searchRecycleFiles() {
            var fileName = $("#search-file-name").val();
            var validTime = $("#valid-time").val(); // 获取有效时间

            // 调用后端接口时传递validTime参数
            $("#recycle-grid").flexOptions({
                newp: 1,
                params: [
                    {name: 'fileName', value: fileName},
                    {name: 'validTime', value: validTime}
                    // 移除startTime和endTime参数
                ]
            }).flexReload();
        }

        // 修改重置函数
        function resetSearch() {
            $("#search-file-name").val("");
            $("#valid-time").val(""); // 重置有效时间
            $("#recycle-grid").flexOptions({
                newp: 1,
                params: []
            }).flexReload();
        }

    </script>


</head>


<body>
<header>
    <input id="search-file-name" th:placeholder="#{file.name}">

    <input type="number" id="valid-time" th:placeholder="#{recycle.valid.time}" min="1">

    <span th:text="#{recycle.day}"></span>

    <a href="#" class="btn-primary btn" onclick="searchRecycleFiles()">
        <i class="glyphicon glyphicon-search"></i>
        <span th:text="#{common.search}"></span>
    </a>

    <a href="#" class="btn-default btn" onclick="resetSearch()">
        <i class="glyphicon glyphicon-refresh"></i>
        <span th:text="#{common.reset}"></span>
    </a>

    <a href="#" sec:authorize="hasRole('ROLE_cldk_file_recover')" class="btn-success btn" onclick="batchRecoverFiles()">
        <i class="glyphicon glyphicon-repeat"></i>
        <span th:text="#{recycle.batch.recover}"></span>
    </a>

    <a href="#" sec:authorize="hasRole('ROLE_cldk_file_force_del')" class="btn-danger btn" onclick="batchPermanentDelete()">
        <i class="glyphicon glyphicon-trash"></i>
        <span th:text="#{recycle.batch.permanent.delete}"></span>
    </a>

    <a href="#" sec:authorize="hasRole('ROLE_cldk_file_force_del')" class="btn-warning btn" onclick="emptyRecycleBin()">
        <i class="glyphicon glyphicon-trash"></i>
        <span th:text="#{recycle.empty}"></span>
    </a>

    <a href="#" class="btn-info btn" th:href="@{/file/fileAll}">
        <i class="glyphicon glyphicon-folder-open"></i>
        <span th:text="#{file.return.list}"></span>
    </a>
</header>

<!-- 表格区域-->
<table id="recycle-grid"></table>

<!-- 对话框区域 -->
<div id="permanent-delete-dialog" sec:authorize="hasRole('ROLE_cldk_file_force_del')" style="display: none">
    <p><span th:text="#{recycle.permanent.delete.confirm.message}"></span></p>
    <p><span id="permanent-delete-file-name"></span></p>
    <input type="hidden" id="permanent-delete-file-id" value="">
</div>

<div id="empty-recycle-confirm-dialog" sec:authorize="hasRole('ROLE_cldk_file_force_del')" style="display: none">
    <p><span th:text="#{recycle.empty.confirm.message}"></span></p>
</div>

</body>
</html>